function [x,term]=broyden(f,x0,varargin)
%
% function [x,term] = broyden(f,x0,P1,P2,...);
%
% f         : function for which we want to find a zero
% x0        : initial condition for x
% P1,...    : parameters of the function
%
% x         : solution
% Term      : Termination status (1->OK, 0-> Failure)
% x和y是列向量
eps1    = 1e-8;
eps2    = 1e-8;
eps    = 1e-12;
x0      = x0(:);
y0      = feval(f,x0,varargin{:});
S       = eye(size(x0,1));

err = 1;
iter    = 1;
while err>eps
   d  	= -S\y0;
   x    = x0+d;
   y    = feval(f,x,varargin{:});
   S    = S+((y-y0)-S*d)*d'/(d'*d);
   err  = norm(x-x0,2)-eps1*(1+norm(x,2));
   ferr = norm(y,2);
   x0   = x;
   y0   = y;
   iter = iter+1;
end
if ferr<eps2
   term = 1;    
else
   term = 0;    
end
